<?php

namespace IDock\Insurance\ZSIC;

use IDock\Http\Response;
use IDock\IDock\IDockInterface;
use IDock\Insurance\ZSIC\Request\ElecPolicyDownloadRequest;
use IDock\Insurance\ZSIC\Request\NonCarProductListRequest;
use IDock\Insurance\ZSIC\Request\PolicyPaymentRequest;
use IDock\Insurance\ZSIC\Request\PremiumInquiryRequest;
use IDock\Insurance\ZSIC\Request\RenewalInquiryRequest;
use IDock\Insurance\ZSIC\Request\UnderwritingRecall2Request;
use IDock\Insurance\ZSIC\Request\UnderwritingRecallRequest;
use IDock\Insurance\ZSIC\Request\UnderwritingRequest;
use IDock\Insurance\ZSIC\Request\UploadFileRequest;
use IDock\Insurance\ZSIC\Request\VehicleActualValueRequest;
use IDock\Insurance\ZSIC\Request\VehicleCheckRequest;
use IDock\Insurance\ZSIC\Request\VehicleModelRequest;
use IDock\Insurance\ZSIC\Response\ElecPolicyDownloadResponse;
use IDock\Insurance\ZSIC\Response\NonCarProductListResponse;
use IDock\Insurance\ZSIC\Response\PolicyPaymentResponse;
use IDock\Insurance\ZSIC\Response\PremiumInquiryResponse;
use IDock\Insurance\ZSIC\Response\RenewalInquiryResponse;
use IDock\Insurance\ZSIC\Response\UnderwritingRecallResponse;
use IDock\Insurance\ZSIC\Response\UnderwritingResponse;
use IDock\Insurance\ZSIC\Response\UploadFileResponse;
use IDock\Insurance\ZSIC\Response\ValidateCarResponse;
use IDock\Insurance\ZSIC\Response\VehicleActualValueResponse;
use IDock\Insurance\ZSIC\Response\VehicleCheckResponse;
use IDock\Insurance\ZSIC\Response\VehicleModelResponse;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Visitor\EPolicyDownloadVisitor;
use IDock\Visitor\NonCar\NonCarInquiryVisitor;
use IDock\Visitor\NonCar\NonCarProductListVisitor;
use IDock\Visitor\NonCar\NonCarProductVisitor;
use IDock\Visitor\PolicyInfoVisitor;
use IDock\Visitor\PolicyPaymentVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\ProposalRecallVisitor;
use IDock\Visitor\ProposalStatusVisitor;
use IDock\Visitor\RenewalInquiryVisitor;
use IDock\Visitor\UnderwritingRecallVisitor;
use IDock\Visitor\UploadFileVisitor;
use IDock\Visitor\ValidateCarVisitor;
use IDock\Visitor\VehicleActualValueVisitor;
use IDock\Visitor\VehicleModelVisitor;

/**
 * 太平保险
 */
class ZSICConstruct extends Builder implements IDockInterface
{

    protected $config = [];

    public function __construct($config)
    {
        $this->config = $config;
        $this->clientName = $config['clientName'] ?? '';
        $this->accessKey = $config['accessKey'] ?? '';
        $this->gateway = $config['gateway'] ?? '';
    }

    /**
     * 车型查询
     * @param VehicleModelVisitor $vehicleModelVisitor
     * @return VehicleModelResponse
     */
    public function vehicleModel(VehicleModelVisitor $vehicleModelVisitor): VehicleModelResponse
    {
        $request = new VehicleModelRequest();
        $request->setVisitor($vehicleModelVisitor);
        /*** @var VehicleModelResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 续保查询
     * @param RenewalInquiryVisitor $renewalInquiryVisitor
     * @return RenewalInquiryResponse
     */
    public function renewalInquiry(RenewalInquiryVisitor $renewalInquiryVisitor): RenewalInquiryResponse
    {
        $request = new RenewalInquiryRequest();
        $request->setVisitor($renewalInquiryVisitor);

        /*** @var RenewalInquiryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 车辆实际价格接口
     * @param VehicleActualValueVisitor $vehicleActualValueVisitor
     * @return VehicleActualValueResponse
     */
    public function vehicleActualValue(VehicleActualValueVisitor $vehicleActualValueVisitor): VehicleActualValueResponse
    {
        $request = new VehicleActualValueRequest();
        $request->setVisitor($vehicleActualValueVisitor);

        /*** @var VehicleActualValueResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 询价前置
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     */
    public function preInquiry(PremiumInquiryVisitor $premiumInquiryVisitor)
    {

    }

    public function validateCar(PremiumInquiryVisitor $premiumInquiryVisitor): VehicleCheckResponse
    {
        $checkRequest = new VehicleCheckRequest();
        $checkRequest->setVisitor($premiumInquiryVisitor);
        /*** @var VehicleCheckResponse $response */
        $response = $this->execute($checkRequest);
        if ($response->isSuccess()) {
            $response->printer->ext['cInspectClaim'] = $premiumInquiryVisitor->getExtInfoVisitor()->argc['cInspectClaim'] ?? '';
        }
        return $response;
    }

    /**
     * 询价
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return PremiumInquiryResponse
     */
    public function premiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): PremiumInquiryResponse
    {
        $request = new PremiumInquiryRequest();
        $request->setVisitor($premiumInquiryVisitor);
        /*** @var PremiumInquiryResponse $response */
        $response = $this->execute($request);
        if ($response->isSuccess()) {
            $response->printer->argc['applyLicenseNo'] = $premiumInquiryVisitor->getApplicantVisitor()->certificateNo;
            $response->buildCoverage($premiumInquiryVisitor->getVehicleVisitor()->isNewEnergy, $premiumInquiryVisitor->getCoverageVisitorList());
            //非车
            $nonCarInquiryPrinter = new NonCarInquiryPrinter();
            $totalPremium = 0;
            foreach ($premiumInquiryVisitor->getNonCarInquiryVisitor()->getNonCarList() as $item) {
                /*** @var $item NonCarProductVisitor */
                $single = new NonCarInquirySinglePrinter();
                $single->premium = $item->premium;
                $single->planCode = $item->planCode;
                $single->productCode = $item->productCode;
                $single->statusCode = 'success';
                $totalPremium += $single->premium * $item->insuredNum;
                $nonCarInquiryPrinter->noCarInquirySingleAppend($single);
            }
            $nonCarInquiryPrinter->totalPremium = $totalPremium;
            $response->printer->nonCarInquiryPrinter = $nonCarInquiryPrinter;
        }

        return $response;
    }

    /**
     * 核保
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return UnderwritingResponse
     */
    public function underwriting(PremiumInquiryVisitor $premiumInquiryVisitor): UnderwritingResponse
    {
        $request = new UnderwritingRequest();
        $request->setVisitor($premiumInquiryVisitor);

        /**@var UnderwritingResponse $response * */
        $response = $this->execute($request);
        //上传文件
        if (!empty($premiumInquiryVisitor->getFileList()) && $response->isSuccess()) {
            $printer = $response->printer;
            foreach ($premiumInquiryVisitor->getFileList() as $value) {
                $uploadFileVisitor = new UploadFileVisitor();
                $uploadFileVisitor->argc = array_merge($premiumInquiryVisitor->getExtInfoVisitor()->argc, $printer->argc);
                $uploadFileVisitor->ciInquiryNo = $printer->ciInquiryNo;
                $uploadFileVisitor->biInquiryNo = $printer->biInquiryNo;
                $uploadFileVisitor->inquiryNo = $printer->argc['quotationNo'] ?? '';
                $uploadFileVisitor->fileAppend($value['type'], $value['url'], $value['base64']);
                $this->uploadImg($uploadFileVisitor);
            }
        }
        //非车数据回填重构
//        /*** @var PremiumUnderwritingPrinter $printer */
//        $printer = $response->getPrinter();
//        if ($printer->noCarUnderwritingPrinter->statusCode == 'success' && !empty($premiumInquiryVisitor->getNonCarUnderwritingVisitor()->getNonCarList())) {
//
//            /*** @var NonCarProductVisitor $nonProduct */
//            $nonProduct = $premiumInquiryVisitor->getNonCarUnderwritingVisitor()->getNonCarList()[0];
//
//            $nonCarList = $printer->noCarUnderwritingPrinter->getNonCarInquiryList();
//
//            foreach ($nonCarList as &$value) {
//                /*** @var NonCarInquirySinglePrinter $value */
//                $value->productCode = $nonProduct->productCode;
//                $value->planCode = $nonProduct->planCode;
//            }
//        }
//
//        $response->printer = $printer;
        return $response;
    }

    /**
     * 获取支付链接
     * @param PolicyPaymentVisitor $paymentVisitor
     * @return PolicyPaymentResponse
     */
    public function policyPayment(PolicyPaymentVisitor $paymentVisitor): PolicyPaymentResponse
    {
        $request = new PolicyPaymentRequest();
        $request->setVisitor($paymentVisitor);

        /**@var PolicyPaymentResponse $response * */
        $response = $this->execute($request);

        return $response;
    }

    public function proposalStatus(ProposalStatusVisitor $proposalStatusVisitor)
    {

    }

    /**
     * 核保撤销
     * @param ProposalRecallVisitor $revokeVisitor
     * @return UnderwritingRecallResponse
     */
    public function proposalRecall(ProposalRecallVisitor $revokeVisitor): UnderwritingRecallResponse
    {
        $request = new UnderwritingRecallRequest();
        $request->setVisitor($revokeVisitor);

        /*** @var UnderwritingRecallResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 撤单接口
     * @param UnderwritingRecallVisitor $recallVisitor
     * @return UnderwritingRecallResponse
     */
    public function underwritingRecall(UnderwritingRecallVisitor $recallVisitor): Response
    {
        $request = new UnderwritingRecall2Request();
        $request->setVisitor($recallVisitor);

        /*** @var UnderwritingRecallResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function EPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor): ElecPolicyDownloadResponse
    {
        $request = new ElecPolicyDownloadRequest();
        $request->setVisitor($EPolicyVisitor);

        /**
         * @var ElecPolicyDownloadResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function policyInfo(PolicyInfoVisitor $policyInfoVisitor)
    {

    }

    public function UploadFile(UploadFileVisitor $uploadFileVisitor): UploadFileResponse
    {
        $response = new UploadFileResponse();
        $response->setCode(200);
        return $response;
    }

    public function uploadImg(UploadFileVisitor $uploadFileVisitor): UploadFileResponse
    {
        $request = new UploadFileRequest();
        $request->setVisitor($uploadFileVisitor);
        /**
         * @var UploadFileResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }


    /**
     * 非车产品列表
     * @param NonCarProductListVisitor $nonCarProductListVisitor
     * @return NonCarProductListResponse
     * @version 4.0
     */
    public function nonCarProductList(NonCarProductListVisitor $nonCarProductListVisitor): NonCarProductListResponse
    {
        $request = new NonCarProductListRequest();
        $request->setVisitor($nonCarProductListVisitor);

        /*** @var NonCarProductListResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function nonCarInquiry(NonCarInquiryVisitor $nonCarInquiryVisitor)
    {
    }

    public function nonCarUnderwriting(NonCarInquiryVisitor $inquiryPrinter)
    {
    }

    /**
     * 验车码接口
     * @param ValidateCarVisitor $validateCarVisitor
     * @return ValidateCarResponse
     */
    public function validateCarCode(ValidateCarVisitor $validateCarVisitor): ValidateCarResponse
    {
        $response = new ValidateCarResponse(200);
        $response->printerInit();
        return $response;
    }

    /**
     * 新车备案
     * @return void
     */
    public function newVehicleRegistration(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }
}
